package com.example.wanandroid.activity.welcome

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

class CustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    deStyleAttr: Int = 0
) : View(context, attrs, deStyleAttr) {
    private var isShow = false
    private var circleX = 0f
    private var circleY = 0f
    private var circleRadius = 10f
    private val paint = Paint().apply {
        style = Paint.Style.FILL
        isAntiAlias = true
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        if (isShow)
            canvas.drawCircle(circleX, circleY, circleRadius, paint)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event != null) {
            isShow = true
            circleX = event.x
            circleY = event.y
            invalidate()
        }

        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                paint.color = Color.RED
            }

            MotionEvent.ACTION_MOVE -> {
                paint.color = Color.CYAN
            }

            MotionEvent.ACTION_UP -> {
                paint.color = Color.BLUE
            }
        }

        return true
    }
}